<template>
  <div class="app-container">
    <el-button type="primary" @click="dialogVisible = true"
      >添加广告类型</el-button
    >
    <el-table
      title="广告类型列表"
      :data="list"
      border
      sortable
      style="width: 100%"
    >
      <el-table-column type="selection" width="40"> </el-table-column>
      <el-table-column type="index" label="序号" sortable width="50">
      </el-table-column>

      <el-table-column sortable prop="title" label="广告类型标题" width="300">
      </el-table-column>

      <el-table-column prop="gmtCreate" label="创建时间" sortable width="200">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="updateInit(scope.row.id)"
            >修改</el-button
          >
          <el-button type="danger" @click="delCourse(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <br />
    <br />
    <el-pagination
      @current-change="handleSizeChange"
      :page-size="page.size"
      background
      layout="prev, pager, next"
      :total="page.total"
    >
    </el-pagination>

    <!--添加广告类型-->
    <el-dialog
      title="保存广告类型"
      :visible.sync="dialogVisible"
      width="30%"
      @before-close="dialogVisible = false"
    >
      <!--表单提交-->
      <el-form
        :model="adType"
        ref="adType"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="广告类型名称" prop="title">
          <el-input v-model="adType.title"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdType()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  createAbType,
  updateAbType,
  findByIdAbType,
  findAllAbType,
  delAbType,
} from "@/api/cms/adType";

export default {
  data() {
    return {
      dialogVisible: false,
      typeLabel: "",
      // 分页
      page: {
        current: 1,
        size: 10,
        total: 0,
      },
      // 修改或者创建广告类型
      adType: {
        title: "",
        id: null,
      },
      // 列表
      list: [],
    };
  },
  created() {
    this.findAll();
  },
  methods: {
    // 修改初始话,回显
    async updateInit(id) {
      this.adType.id = id;
      // 打开窗口
      this.dialogVisible = true;
      // 回显
      let result = await findByIdAbType(id);
      console.log(result);
      if (result.code === 20000) {
        this.adType = result.data;
        return false;
      }
      this.$message.error("加载信息失败，请稍后再试！");
    },
    // 保存广告类型
    async saveAdType() {
      if (this.adType.id) {
        // 修改
        await this.updateAbType();
      } else {
        // 创建
        await this.createAbType();
      }
      // 清空
      this.adType.title = "";
      this.adType.id = null;
      // 关闭窗口
      this.dialogVisible = false;
    },

    displayImage(url) {
      location.href = url;
    },
    // 查询全部
    async findAll() {
      let result = await findAllAbType(this.page.current, this.page.size);
      this.list = result.data.records;
      console.log(this.list);
      // 加载分页
      this.page.current = result.data.current;
      this.page.size = result.data.size;
      this.page.total = result.data.total;
    },
    // 分页改变时
    handleSizeChange(value) {
      this.page.current = value;
      this.findAll();
    },
    // 删除
    async delCourse(id) {
      this.$confirm("确定要删除该广告类型？", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        let r = await delAbType(id);
        if (r.code === 20000) {
          this.$message.success("删除成功!");
          // 刷新
          await this.findAll();
          return false;
        }
        this.$message.error("删除失败，" + r.message);
      });
    },
    // 创建
    async createAbType() {
      let result = await createAbType(this.adType);
      if (result.code === 20000) {
        this.$message.success("保存成功!");
        // 刷新
        await this.findAll();
        return false;
      }
      this.$message.error("保存失败，" + result.message);
    },
    // 修改
    async updateAbType() {
      let result = await updateAbType(this.adType);
      if (result.code === 20000) {
        this.$message.success("保存成功!");
        // 刷新
        await this.findAll();
        return false;
      }
      this.$message.error("保存失败，" + result.message);
    },
  },
};
</script>
